using Aliphant.Mvvm.XEvent.Interface;
using Aliphant.Mvvm.XEvent.Ioc;
using Aliphant.Mvvm.XEvent.Models;
using Aliphant.Mvvm.XEvent.UI;
using GrpcClient.Managers;
using GrpcClient.Models;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace GrpcClient.ViewModels
{
    public class NavigationBarViewModel : BindableBase, INavigate
    {

        private bool isLoad = false;

        public ObservableCollection<NavigateModel> NavigateCollection { get; private set; } = new ObservableCollection<NavigateModel>();

        public ICommand NavigationCommand
        {
            get => new RelayCommand<object>(obj =>
            {
                if (obj is NavigateModel navigate)
                {
                    XContainerLocator.Instance.NavigateTo("body", navigate.ViewName);
                }
            });
        }

        public void OnNavigateTo(NavigateParameter navigateParameter)
        {
            Init();

            if (!isLoad)
            {
                OnSelectedChanaged();
                isLoad = true;
            }
        }

        private void Init()
        {
            NavigateCollection.Clear();
            var models = ConfigContainer.Instance.cacheManager.Get<List<NavigateModel>>("navi");

            if (models != null)
            {
                foreach (var item in models)
                {
                    NavigateCollection.Add(item);
                }
            }
        }

        private void OnSelectedChanaged()
        {
            var navigate = NavigateCollection[0];
            navigate.IsChecked = true;
            XContainerLocator.Instance.NavigateTo("body", navigate.ViewName);
        }

    }
}
